package com.cw.jeeyt.service.impl.git;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cw.jeeyt.service.bo.git.GitBranchBo;
import com.cw.jeeyt.dao.intf.git.GitBranchDao;
import com.cw.jeeyt.dao.model.git.GitBranchDo;
import com.cw.jeeyt.dao.query.git.GitBranchQuery;
import com.cw.jeeyt.service.intf.git.GitBranchService;
import com.cw.jeeyt.service.converter.git.GitBranchConverter;
import com.cw.lang.mybatis.dto.PageInfo;
import com.cw.lang.mybatis.dto.PageParam;

/**
 * git项目分支信息接口业务实现类
 *
 * @author chenw
 * @date 2019-11-24 16:15:01
 */
@Service
public class GitBranchServiceImpl implements GitBranchService {
    @Autowired
    private GitBranchDao gitBranchDao;

    /**
     * 新增git项目分支信息
     *
     * @param request 条件
     * @return String
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addGitBranch(GitBranchBo request) {
        if (request == null) {
            return false;
        }
        gitBranchDao.insertBySelective(GitBranchConverter.bo2Do(request));
        return true;
    }

    /**
     * 更新git项目分支信息
     *
     * @param request 条件
     * @return boolean
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean modifyGitBranch(GitBranchBo request) {
        if (request == null) {
            return false;
        }
        gitBranchDao.updateBySelective(GitBranchConverter.bo2Do(request));
        return true;
    }

    /**
     * 删除git项目分支信息
     *
     * @param request 条件
     * @return boolean
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteGitBranch(GitBranchBo request) {
        if (request == null) {
            return false;
        }
        return gitBranchDao.deleteByKey(request.getId()) > 0;
    }

    /**
     * 分页查询git项目分支信息
     *
     * @param request 条件
     * @return PageInfo<GitBranchBo>
     */
    @Override
    public PageInfo<GitBranchBo> selectPage(GitBranchBo request, PageParam pageParam) throws Exception {
        PageInfo<GitBranchDo> pageInfo = gitBranchDao.selectPage(GitBranchQuery.builder()
            .id(request.getId())
            .projectId(request.getProjectId())
            .branchCode(request.getBranchCode())
            .branchName(request.getBranchName())
            .build(), pageParam);
        List<GitBranchBo> gitBranchs = GitBranchConverter.doList2BoList(pageInfo.getList());
        return new PageInfo<>(pageInfo, gitBranchs);
    }

    /**
     * 查询git项目分支信息详情信息
     *
     * @param request 条件
     * @return GitBranchBo
     */
    @Override
    public GitBranchBo queryGitBranchDetail(GitBranchBo request) {
        if (request == null) {
            return null;
        }
        return GitBranchConverter.do2Bo(gitBranchDao.selectByKey(request.getId()));
    }

    /**
     * 根据条件查询git项目分支信息
     *
     * @param request 条件
     * @return List<GitBranchBo>
     */
    @Override
    public List<GitBranchBo> queryGitBranchList(GitBranchBo request) {
        if (request == null) {
            return null;
        }
        return GitBranchConverter.doList2BoList(gitBranchDao.selectByExample(GitBranchQuery.builder()
            .id(request.getId())
            .projectId(request.getProjectId())
            .branchCode(request.getBranchCode())
            .branchName(request.getBranchName())
            .build()));
    }

    /**
     * 根据key查询git项目分支信息
     *
     * @param id
     * @return
     */
    @Override
    public GitBranchBo selectByKey(Long id) {

        return GitBranchConverter.do2Bo(gitBranchDao.selectByKey(id));
    }
}
